<?php

/**
 * periodos actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage periodos
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class periodosActions extends sfActions {

    public $fechain;

    public function executeIndex(sfWebRequest $request) {
        $this->periodos = Doctrine_Core::getTable('periodo')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->periodo = Doctrine_Core::getTable('periodo')->find(array($request->getParameter('id_pe')));
        $this->forward404Unless($this->periodo);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new periodoForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new periodoForm();
        $this->processForm($request, $this->form);
        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($periodo = Doctrine_Core::getTable('periodo')->find(array($request->getParameter('id_pe'))), sprintf('Object periodo does not exist (%s).', $request->getParameter('id_pe')));
        $this->form = new periodoForm($periodo);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($periodo = Doctrine_Core::getTable('periodo')->find(array($request->getParameter('id_pe'))), sprintf('Object periodo does not exist (%s).', $request->getParameter('id_pe')));
        $this->form = new periodoForm($periodo);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($periodo = Doctrine_Core::getTable('periodo')->find(array($request->getParameter('id_pe'))), sprintf('Object periodo does not exist (%s).', $request->getParameter('id_pe')));
        $periodo->delete();

        $this->redirect('periodos/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            
            $periodo = $form->save();
            $fi = explode("-", $periodo->getFechai());
            $ff = explode("-", $periodo->getFechaf());
            $ai = $fi[0];
            $mi = $fi[1];
            $af = $ff[0];
            $mf = $ff[1];
            $resul = 0;
            if ($ai == $af) {
                if ($mi == 01 && $mf == 12) {
                    $resul = 1;
                }else{
                    $resul = 0;
                }
            } else {

                if ($ai < $af) {
                    if ($mi = 02 && $mf == 01) {
                        $resul = 1;
                    } else if ($mi = 03 && $mf == 02) {
                        $resul = 1;
                    } else if ($mi = 04 && $mf == 03) {
                        $resul = 1;
                    } else if ($mi = 05 && $mf == 04) {
                        $resul = 1;
                    } else if ($mi = 06 && $mf == 05) {
                        $resul = 1;
                    } else if ($mi = 07 && $mf == 06) {
                        $resul = 1;
                    } else if ($mi = 08 && $mf == 07) {
                        $resul = 1;
                    } else if ($mi = 09 && $mf == 08) {
                        $resul = 1;
                    } else if ($mi = 10 && $mf == 09) {
                        $resul = 1;
                    } else if ($mi = 11 && $mf == 10) {
                        $resul = 1;
                    } else if ($mi = 12 && $mf == 11) {
                        $resul = 1;
                    } else if ($mi = 01 && $mf == 12) {
                        $resul = 1;
                    } else {
                        $resul = 0;
                    }
                } else if ($af - $ai >= 2) {
                    $resul = 1;
                } else {
                    $resul = 0;
                }
            }

            if ($resul == 1) {                
                $this->redirect('periodos/edit?id_pe=' . $periodo->getIdPe());
            } else {
                $periodo->delete();
                $this->redirect('periodos/new');
            }
        }
    }

}

